<template>
  <div class="container">
    <div v-if="courses.length > 0">
      <!-- 课程信息 -->
      <div class="course-list">
        <div class="course-item" v-for="course in courses" :key="course.id">
          <img
            v-if="course.coverPath"
            :src="course.coverPath"
            class="course-image"
            alt="课程图片"
          />
          <div v-else class="default-img" @click="toCoursePage">{{ course.chapterName }}</div>
          <div class="info">
            <h4>{{ course.chapterName }}</h4>
            <p>{{ course.teacherName }}</p>
          </div>
        </div>
      </div>

      <!-- 互动工具 -->
      <div class="chat-center">
        <h3><strong>互动工具</strong></h3>
        <div class="center-grid">
          <div class="center-item" style="background-color: #eff6ff" @click="raiseHand">
            <img src="@/assets/img/handup.svg" alt="举手" />
            <p>举手</p>
          </div>
          <div class="center-item" style="background-color: #f0fdf4">
            <img src="@/assets/img/vote.svg" alt="投票" />
            <p>投票</p>
          </div>
          <div class="center-item" style="background-color: #faf5ff">
            <img src="@/assets/img/discuss.svg" alt="讨论" />
            <p>讨论</p>
          </div>
          <div class="center-item" style="background-color: #fdf2f8">
            <img src="@/assets/img/demoVideo.svg" alt="示范视频" />
            <p>示范视频</p>
          </div>
        </div>
      </div>

      <!-- 小组练习 -->
      <div class="course-practise">
        <div class="practise-header">
          <!-- <van-icon name="bar-chart-o" size="20" /> -->
          <h3><strong>小组练习</strong></h3>
        </div>
        <div class="practise-list" v-if="practises.length > 0">
          <div
            class="practise-item"
            v-for="(item, index) of practises"
            :key="index"
            @click="toDetail(item, index)"
          >
            <div class="practise-index">
              {{ item.groupNumber }}
            </div>
            <div class="content">
              <h4>{{ item.groupingName }}</h4>
              <div class="details">
                <span>{{ item.groupingCount }}人</span>
              </div>
            </div>
            <van-icon name="arrow" class="arrow-icon" />
          </div>
        </div>
        <div v-else class="no-data">暂无数据</div>
      </div>
    </div>
    <div class="no-course-list" v-else>
      <img :src="book" class="book-image" alt="暂无课程图片" />
      <div class="info">暂无进行中的课程</div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from 'vue-router'
import useUserStore from '@/stores/user'
import { getCourseInProgress, getStudentGroupRequest, raiseHandRequest } from '@/api/course'
import book from '@/assets/img/book.svg'
import course1 from '@/assets/img/course1.png'

const router = useRouter()
const userStore = useUserStore()
const userInfo = userStore.userInfo

const courses = ref([
  // {
  //   id: 1,
  //   image: course1,
  //   title: '篮球基础训练',
  //   teacher: '张老师',
  // },
])
const practises = ref([])
const toDetail = (item: any, index: number) => {
  router.push(`/practiseDetail?index=${index}&groupingId=${item.groupingId}`)
}
const getStudentStudentList = async () => {
  const params = {
    chapterId: courses.value[0].chapterId,
    courseId: courses.value[0].courseId,
    classId: courses.value[0].classId,
  }
  const res = await getStudentGroupRequest(params)
  practises.value = res.data
}
const raiseHand = async () => {
  const params = {
    id: courses.value[0].chapterId,
    courseId: courses.value[0].courseId,
    classId: courses.value[0].classId,
    teacherId: courses.value[0].teacherId,
  }
  const res = await raiseHandRequest(params)
  if ((res.code = 200)) {
    showSuccessToast('提交成功！')
  } else {
    showFailToast(res.msg)
  }
}
onActivated(async () => {
  const { data } = await getCourseInProgress()
  courses.value = data.listCourse
  getStudentStudentList()
})
</script>

<style scoped lang="scss">
.container {
  padding: 15px;
  background: #f7f8fa;
  height: calc(100% - 50px);
}

.course-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
  .course-item {
    background-color: #fff;
    padding: 10px;
  }

  .course-item img {
    width: 100%;
    height: 224px;
    object-fit: contain;
    border-radius: 4px;
  }
  .default-img {
    width: 100%;
    height: 160px;
    border-radius: 8px;
    margin-right: 15px;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #d9d9d9;
    margin-bottom: 15px;
  }
  .info {
    font-size: 14px;
    color: #6b7280;
    margin-top: 14px;
  }
}
.no-course-list {
  padding: 50px 28px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .info {
    font-size: 12px;
    color: #6b7280;
    margin-top: 14px;
  }
}

// .info {
//   text-align: center;
// }

.name {
  font-size: 18px;
  margin: 10px 0;
  font-weight: bold;
}

.details {
  font-size: 14px;
  color: #666;
  .van-icon-clock {
    margin-right: 3px;
    color: #3b82f6;
  }
  .van-icon-user {
    color: #22c55e;
    margin-left: 8px;
    margin-right: 3px;
  }
}

.chat-center {
  margin: 30px 0;
}

.chat-center h3 {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}

.center-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 15px;
  padding: 20px;
  background: #fff;
  border-radius: 12px;
}

.center-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 5px;
  border-radius: 8px;
}

.center-item img {
  width: 20px;
  height: 20px;
  margin-bottom: 10px;
}

.center-item p {
  font-size: 12px;
  font-weight: bold;
}

.course-practise {
  margin: 30px 0;
}
.no-data {
  width: 100%;
  height: 80px;
  font-size: 16px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  text-align: center;
  line-height: 80px;
}
.course-practise h3 {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}

.practise-list {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
}

.practise-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #f5f5f5;
  .practise-index {
    width: 35px;
    height: 35px;
    line-height: 35px;
    text-align: center;
    border-radius: 18px;
    background: #dbeafe;
    font-size: 12px;
    color: #3b82f6;
    margin-right: 10px;
  }

  .arrow-icon {
    color: #3b82f6;
    font-size: 14px;
  }
}

.course-img {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  margin-right: 15px;
  object-fit: cover;
}

.content {
  flex: 1;
}

.content h4 {
  font-size: 15px;
  font-weight: bold;
  margin-bottom: 5px;
}

.content p {
  font-size: 12px;
  color: #666;
}
</style>
